package com.example.chaindemo.web.chain;

import com.example.chaindemo.provider.validator.data.ValidData;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author PC3
 */
@Component
public class ValidatorHandler {

    private List<Validator> validatorList;

    public ValidatorHandler(@Autowired List<Validator> validators) {
        this.validatorList = validators;
        Collections.sort(this.validatorList, AnnotationAwareOrderComparator.INSTANCE);
    }

    public void valid(ValidData<String> data){
        for (Validator validator : validatorList) {

            if(validator.enabled()) {
                validator.valid(data);
            }

        }
    }
}
